import {
    createRouter,
    createWebHashHistory,
    createWebHistory
} from 'vue-router';
import {
    useCounterStore
} from '@/pinia/user'
/*浏览器上面进度条*/
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

NProgress.configure({
    showSpinner: false, // 是否显示加载ico
});

const routes = [{
        path: '/',
        redirect: to => {
            // 方法接收目标路由作为参数
            // return 重定向的字符串路径/路径对象
            return {
                path: '/login'
            }
        },
    },
    {
        path: '/login',
        name: 'login',
        component: () => import('@/views/login/index.vue')
    },
    {
        path: '/home',
        name: '首页',
        component: () => import( /* webpackChunkName: "introduce" */ '@/views/homePage/index.vue')
    },
    {
        path: '/fillin',
        name: 'fillin',
        meta: {
            keepAlive: true
        },
        // name: '填写入职信息',
        component: () => import( /* webpackChunkName: "introduce" */ '@/views/FillIn/fillin.vue')
    },
    {
        path: '/querystatus',
        name: 'querystatus',
        component: () => import( /* webpackChunkName: "introduce" */ '@/views/QueryStatus/index.vue')
    },
    {
        path: '/address',
        name: 'address',
        component: () => import( /* webpackChunkName: "introduce" */ '@/views/address/index.vue')
    },
    {
        path: '/AddressEdit',
        name: 'AddressEdit',
        component: () => import( /* webpackChunkName:'introduce" */ '@/views/AddressEdit/index.vue')

    },
    {
        path: '/demo',
        name: '测试',
        component: () => import( /* webpackChunkName: "introduce" */ '@/views/demo/index.vue')
    },
]

const router = createRouter({
    history: createWebHistory(
        import.meta.env.VITE_APP_STORAGE_PREFIX),
    routes
});

router.beforeEach((to, from, next) => {
    const CounterStore = useCounterStore()
    //如果设置标题就显示，没设置使用默认
    if (null != to.meta && null != to.meta.title && '' !== to.meta.title) {
        document.title = to.meta.title ||
            import.meta.env.VITE_APP_TITLE;
    } else {
        document.title =
            import.meta.env.VITE_APP_TITLE;
    }
    //浏览器上方显示进度条
    NProgress.start();
    if (to.path !== '/login') {
        if (!CounterStore.mobile) next('/')
    }
    //正常放行
    next();
});

router.afterEach(transition => {
    //关闭浏览器上方的进度条
    NProgress.done();
});

export default router;